#include "Germanium.hh"
using namespace std;

void Germanium::SetCore(int crystal, double energy, MyTime_t time){
  for(int i=0;i<fCrystal.size();i++){//check if crystal was already set
    //cout << "fCrystal[i].GetCryID() " << fCrystal[i].GetCryID() << endl;
    if(crystal==fCrystal[i].GetCryID()){
      //cout << "Core " << fClusterID << " Crystal " << i << " was already set." << endl;
      fCrystal[i].SetCore(energy, time);
      strange =1;
      return;
    }
  }
  //cout << "Germanium::SetCore with crystal" << crystal << " energy " << energy << " time " << time << endl;
  
  strange =0;
  if(crystal <0 || crystal >2)
    cout << "crystal id in Germanium::SetCore " << crystal << ", " <<energy << ", " << time << endl;
  fCrystal.push_back(Crystal(crystal, energy, time));
}
void Germanium::SetSegment(int crystal, int segment, double energy, MyTime_t time){
  if(crystal <0 || crystal >2)
    cout << "strange crystal id in Germanium::SetSegment " << crystal << endl;

  for(int i=0;i<fCrystal.size();i++){//check if crystal was already set
    if(crystal==fCrystal[i].GetCryID()){
      fCrystal[i].AddSegment(segment, energy, time);
      return;
    }
  }
  fCrystal.push_back(Crystal(crystal, segment, energy, time));
}
void Germanium::PrintCluster(){
  cout << "size " << fCrystal.size() << endl;
  cout << "Cluster ID\t" << GetCluID() << endl;
  for(int i=0;i<fCrystal.size();i++){
    cout << "Crystal ID\t" << fCrystal[i].GetCryID() << "\tTime\t" << fCrystal[i].GetTime() << "\tCore Energy\t" << fCrystal[i].GetCore() << endl;
    for(int j=0; j<fCrystal[i].GetSegID().size();j++ ){
      cout << "Segment ID\t" << fCrystal[i].GetSegID()[j] << "\tSegment Energy\t" << fCrystal[i].GetSeg()[j] << endl;
    }
  }
  cout << "--------------------------------------------------" << endl;

}
Crystal::Crystal(int crystal, double energy, MyTime_t time){
  Clear();
  if(crystal <0 || crystal >2){
    cout << "crystal id in Crystal::Crystal(int crystal, double energy, MyTime_t time) " << crystal << ", " << energy << ", " << time << endl;
    cout << "----------------------------------" << endl;
  }
  SetCryID(crystal);
  SetCore(energy, time);
  //cout << "Crystal::Crystal set to " << GetCryID() << " " << GetCore() << " keV " << GetTime() << endl;
}
Crystal::Crystal(int crystal, int segment, double energy, MyTime_t time){
  Clear();
  if(crystal <0 || crystal >2)
    cout << "strange crystal id in Crystal::Crystal(int crystal, int segment, double energy, MyTime_t time) " << crystal << endl;

  SetCryID(crystal);
  AddSegment(segment, energy, time);

}
void Crystal::SetCore(double energy, MyTime_t time){
  //core
  if(fCoreEnergy>0.1){
    //cout << "Core Energy was already set to " << fCoreEnergy << " got new value " << energy << " not changing" << endl;
    //cout << "time was " << fTime << " new time " << time << " diff " << fTime - time << endl;
    return;
  }
  fCoreEnergy = energy;
  /*
  if(fTime>0.1 && time !=fTime){
    //cout << "Core Time was already set to " << fTime << " got new value " << time << " changing now. " << endl;
  }
  */
  fTime = time;
  //if(fTime<0.1 || fTime>1e15)
  //  cout << "strange time " << fTime << endl;
  if(fTime>1e15)
    cout << "strange time " << fTime << endl;
}
void Crystal::AddSegment(int segment, double energy, MyTime_t time){
  //segment
  for(int i=0;i<fSegID.size();i++){//check if segment was already set
    if(segment==fSegID[i]){
      cout << "Segment " << i << " was already was already set to " << fSegEnergy[i] << " got new value of " << energy << " not changing." << endl;
      return;
    }
  }
  fTime = time;
  fSegID.push_back(segment);
  fSegEnergy.push_back(energy);

}
